#!/bin/sh
# rebuild
# (c) 2002 Karel 'Clock' Kulhavy, Mikulas Patocka, Petr 'Brain' Kulhavy
# This file is a part of the Links program, released under GPL.

make=make
if which gmake 2>/dev/null >/dev/null && gmake --version | grep -i "GNU Make" >/dev/null
then
echo Found GNU Make under gmake. Going to use gmake instead of make.
make=gmake
fi

if which autoheader-2.13 2>/dev/null >/dev/null
then
autoheader="autoheader-2.13"
else
autoheader="autoheader"
fi

if [ "$1" = reconf ]; then
	if ! which pkg-config 2>/dev/null >/dev/null
	then
	echo Warning: You don\'t have pkg-config. The PKG_CHECK_MODULES macro will probably fail. >&2	
	fi

	if ! which autoconf 2>/dev/null >/dev/null
	then
		echo You don\'t have autoconf. Wiping out. >&2
		exit 1
	fi

	if ! autoconf --version | grep -i "GNU Autoconf" >/dev/null
	then
		echo This looks like autoconf installed on OpenBSD. >&2
		if ! AUTOCONF_VERSION=2.13 autoconf --version 2>/dev/null >/dev/null
		then
			echo Autoconf 2.13 is not found. This script needs at most autoconf 2.13. \
			Trying with some other \(random\) autoconf. >&2
			path=`echo "$PATH" | tr -s ':' '\n'`
			alt=`find $path -name 'autoconf*' -and ! -name autoconf | head -n 1`
			if [ -z "$alt" ]
				then
				echo Cannot find the real autoconf >&2
				exit 1
			fi
			export AUTOCONF_VERSION=`echo $alt | sed -e 's/^.*autoconf-//g'`
			echo Found $alt. Setting AUTOCONF_VERSION to $AUTOCONF_VERSION.>&2
		else
			echo Found Autoconf 2.13. It should work. >&2
			export AUTOCONF_VERSION=2.13
		fi
	fi

	if ! automake --version | grep -i "GNU automake" >/dev/null
	then
		echo This looks like automake installed on OpenBSD. >&2
		export AUTOMAKE_VERSION=1.4
		if ! automake --version 2>/dev/null >/dev/null
		then
			echo Automake 1.4 is needed. Wiping out. >&2
			exit 1
		fi
		echo Found Automake 1.4. It should work. >&2
	fi
fi

rm -f config.h Makefile config.cache &&
if [ "$1" = reconf ]; then
	echo NEPOUZIVAT pokud nebylo zmeneno configure.in nebo Makefile.am !!! &&
        echo DO NOT use unless configure.in or Makefile.am has been changed !!! &&
	echo rebuild reconf se NESMI prerusit protoze pak by pri nasledujicim commitu byly commitnuty spatne dependence a pri kompilaci by tise vznikaly zahadne neodladitelne chyby!!! &&
	echo rebuild reconf MUST NOT be interrupted because after the following commit bad dependencies would be commited and during compilation, mysterious and undebuggable bugs would originate!!! &&
	echo Timto mistoprisezne prohlasuji ze toto skutecne nastalo a ze proces neprerusim. &&
	echo I hereby affirm that this really has happened and I won\'t break the process. &&
	read kecy &&
	if [ "$kecy" = "ano" -o "$kecy" = "Ano" -o "$kecy" = "yes" -o "$kecy" = "Yes" ]; then
		echo Zodpovednost prijata. &&
		echo Responsibility accepted. &&
		rm -rf .deps missing depcomp aclocal.m4 &&
		$autoheader &&
		rm -f autoh* &&
		aclocal &&
		automake --add-missing &&
		sed 's%sed '\''s/\\\.am\$/\.in/'\''%sed '\''s/\\\.[aA][mM]\$/\.in/'\''%' <missing >missing.tmp &&
		mv -f missing.tmp missing &&
		chmod a+x missing &&
		autoconf
	else
		exit
	fi
fi &&
if [ "$1" = reconf_aby_to_brain_nepouzival ]; then
	echo "Mikulasi, vyser si voko!" &&
	exit
fi &&
#cd Unicode;./gen; cd ..
#cd intl; ./synclang; cd ..
export CFLAGS="$CFLAGS -Wall -g -O2 -pipe -ansi -U__STRICT_ANSI__ -fno-common" &&
#export CFLAGS="-Wall -g -O2 -pipe"
export CXXFLAGS="$CXXFLAGS -Wall -g -O2 -pipe" &&
./configure --enable-graphics --enable-javascript &&
$make clean &&
$make &&
if [ "$1" = reconf ]; then
	$autoheader &&
	automake --add-missing --include-deps --build-dir=. --srcdir-name=. --output-dir=. &&
	sed 's%sed '\''s/\\\.am\$/\.in/'\''%sed '\''s/\\\.[aA][mM]\$/\.in/'\''%' <missing >missing.tmp &&
	mv -f missing.tmp missing &&
	chmod a+x missing &&
	sed -n '{h;s/X11R6/X11R7/p;x;p;}' <configure >configure.tmp &&
	mv -f configure.tmp configure &&
	chmod a+x configure &&
	./configure --enable-graphics --enable-javascript
	if [ $? != 0 ]; then echo CONFIGURE FAILED; exit 1; fi
fi
